//
//  PYCommunityController.m
//  StudyRN
//
//  Created by 朱吉峥 on 2020/2/13.
//  Copyright © 2020 朱吉峥. All rights reserved.
//

#import "PYCommunityController.h"
#import <React/RCTRootView.h>
#import "PYRNBridge.h"
#import "PYRNEvent.h"
#import "PYShowMediaController.h"
#import "PYMediaInfo.h"

@interface PYCommunityController ()

@property (nonatomic, strong) RCTRootView *rootView;

@end

@implementation PYCommunityController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self initRootView];
}

- (void)initRootView
{
    self.rootView = [[RCTRootView alloc] initWithBridge:[PYRNBridge sharedBridge]
                                                     moduleName:@"Community"
                                              initialProperties:nil];
    [self.view addSubview:self.rootView];
    [self.rootView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.left.right.mas_equalTo(0);
    }];
}

- (void)viewSafeAreaInsetsDidChange
{
    [super viewSafeAreaInsetsDidChange];
    [self.rootView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(-self.view.safeAreaInsets.bottom);
    }];
}

- (void)addNotifications
{
    [super addNotifications];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(pushNextControllerNotification:) name:@"PYRNPushToNextControllerNotification" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(shareNotification:) name:@"PYRNShareNotification" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moreNotification:) name:@"PYRNMoreNotification" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(showImgNotification:) name:@"PYRNShowImgNotification" object:nil];
}

- (void)showImgNotification:(NSNotification *)notify
{
//    NSDictionary *dict = [notify object];
    NSDictionary *userInfo = [notify userInfo];
    NSArray *imgArr = userInfo[@"imgs"];
    NSInteger index = [userInfo[@"idx"] integerValue];
    CGFloat radius = [userInfo[@"radius"] floatValue];
    NSLog(@"imgArr = %@", imgArr);
    NSMutableArray<PYMediaInfo *> *modelArray = [NSMutableArray array];
    for (NSDictionary *dic in imgArr) {
        NSString *imgUrl = dic[@"url"];
        NSString *frame = dic[@"frame"];
        NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:imgUrl]];
        NSCachedURLResponse *response = [[NSURLCache sharedURLCache] cachedResponseForRequest:request];
        UIImage *image = [UIImage imageWithData:response.data];
        PYMediaInfo *mediaInfo = [PYMediaInfo infoWithImgUrl:imgUrl];
        mediaInfo.placeholderImg = image;
        mediaInfo.options = kNilOptions;
        mediaInfo.imgViewFrame = CGRectFromString(frame);
        mediaInfo.imgViewCornerRadius = radius;
        [modelArray addObject:mediaInfo];
    }
    PYShowMediaController *controller = [PYShowMediaController controllerWithArray:modelArray index:index];
    controller.modalPresentationStyle = UIModalPresentationFullScreen;
    [self presentViewController:controller animated:YES completion:nil];
}

- (void)pushNextControllerNotification:(NSNotification *)notify
{
    NSDictionary *dict = [notify object];
    NSString *name = dict[@"title"];
    NSLog(@"pushNextControllerNotification，name=%@", name);
    PYViewController *controller = [PYViewController new];
    controller.title = name;
    [self.navigationController pushViewController:controller animated:YES];
}

- (void)shareNotification:(NSNotification *)notify
{
    NSDictionary *dict = [notify object];
    NSString *name = dict[@"title"];
    UIAlertController *controller = [UIAlertController alertControllerWithTitle:name message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    UIAlertAction *shareAction = [UIAlertAction actionWithTitle:@"分享" style:UIAlertActionStyleDefault handler:nil];
    [controller addAction:shareAction];
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
    [controller addAction:cancelAction];
    controller.popoverPresentationController.sourceView = self.view;
    controller.popoverPresentationController.sourceRect = CGRectMake(self.view.width / 2, self.view.height / 2, 1, 1);
    controller.popoverPresentationController.permittedArrowDirections = 0;
    [self presentViewController:controller animated:YES completion:nil];
}

- (void)moreNotification:(NSNotification *)notify
{
    NSDictionary *dict = [notify object];
    NSString *name = dict[@"title"];
    UIAlertController *controller = [UIAlertController alertControllerWithTitle:name message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    UIAlertAction *action0 = [UIAlertAction actionWithTitle:@"不感兴趣" style:UIAlertActionStyleDefault handler:nil];
    [controller addAction:action0];
    UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"举报该帖" style:UIAlertActionStyleDefault handler:nil];
    [controller addAction:action1];
    UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"屏蔽该用户" style:UIAlertActionStyleDefault handler:nil];
    [controller addAction:action2];
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
    [controller addAction:cancelAction];
    controller.popoverPresentationController.sourceView = self.view;
    controller.popoverPresentationController.sourceRect = CGRectMake(self.view.width / 2, self.view.height / 2, 1, 1);
    controller.popoverPresentationController.permittedArrowDirections = 0;
    [self presentViewController:controller animated:YES completion:nil];
}

@end
